<?php
namespace app\admin\controller;
use app\admin\logic\WorkRecordCloudLogic;

/**
 * 工作记录工作云盘api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/10/30
 * Time: 17:23
 */
class WorkRecordCloud extends Base {

    public function __construct(){
        parent::__construct();
        $this->wrc_logic = new WorkRecordCloudLogic();
    }

    /**
     * 获取工作云盘
     * @return \think\response\Json
     */
    public function ajax_get_wrc(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $maps = $this->wrc_logic->get_search_wrc_maps($params['parentid'],$params['keywords']);
        $limit = ($params['page'] - 1) * $params['rows'];

        $list = model('work_record_cloud')->get_list($maps,'id,type,name,size,url,parentid,is_password,password,c_time','type asc,id desc','',$limit,$params['rows']);
        foreach($list as $k => $v){
            $list[$k]['is_password_zw'] = $v->is_password_name;
        }

        $total_num = model('work_record_cloud')->get_count($maps,'id');

        $data['list'] = $list;
        $data['total_num'] = $total_num;
        $data['parentid'] = $params['parentid'];
        return json($this->wrc_logic->get_response(1,'OK',$data));
    }

    /**
     * 新增文件
     * @return \think\response\Json
     */
    public function ajax_add_file(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查新增文件规则
        $r = $this->wrc_logic->check_add_file_rule($params);
        if(!$r['code']){
            return json($r);
        }

        $wrc_data = [];
        $wrc_data['type'] = 2;
        $wrc_data['name'] = trim($params['name']);
        $wrc_data['size'] = get_flie_size($params['size']);
        $wrc_data['url'] = $params['url'];
        $wrc_data['parentid'] = $params['parentid'];
        $r = model('work_record_cloud')->insert($wrc_data);
        if($r){
            return json($this->wrc_logic->get_response(1,'上传成功'));
        }
        return json($this->wrc_logic->get_response(0,'上传失败'));
    }

    /**
     * 新增文件夹
     * @return \think\response\Json
     */
    public function ajax_add_folder(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查新增文件夹规则
        $r = $this->wrc_logic->check_add_foler_rule($params);
        if(!$r['code']){
            return json($r);
        }

        $wrc_data = [];
        $wrc_data['type'] = 1;
        $wrc_data['name'] = trim($params['name']);
        $wrc_data['parentid'] = $params['parentid'];
        $wrc_data['is_password'] = $params['is_password'];
        $wrc_data['password'] = ($params['is_password'] == 1 && !empty(trim($params['password']))) ? trim($params['password']) : '';
        $r = model('work_record_cloud')->insert($wrc_data);
        if($r){
            return json($this->wrc_logic->get_response(1,'创建成功'));
        }
        return json($this->wrc_logic->get_response(0,'创建失败'));
    }

    /**
     * 修改文件夹
     * @return \think\response\Json
     */
    public function ajax_save_folder(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查修改文件夹规则
        $r = $this->wrc_logic->check_save_folder_rule($params);
        if(!$r['code']){
            return json($r);
        }
        $wrc_detail = $r['data']['wrc_detail'];

        $wrc_data = [];
        $wrc_data['name'] = trim($params['name']);
        $wrc_data['is_password'] = $params['is_password'];
        $wrc_data['password'] = ($params['is_password'] == 1 && !empty(trim($params['password']))) ? trim($params['password']) : '';

        $maps = [];
        $maps[] = ['id','=',$wrc_detail['id']];
        $r = model('work_record_cloud')->force()->save($wrc_data,$maps);
        if($r !== false){
            return json($this->wrc_logic->get_response(1,'修改成功'));
        }
        return json($this->wrc_logic->get_response(0,'修改失败'));
    }

    /**
     * 删除文件夹
     * @return \think\response\Json
     */
    public function ajax_del_folder(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $r = model('work_record_cloud')->destroy($params['wrcid']);
        if($r){
            return json($this->wrc_logic->get_response(1,'删除成功'));
        }
        return json($this->wrc_logic->get_response(0,'删除失败'));
    }
}
